<?php

namespace plugin\ycMassage\app\shop\controller;

use app\common\builder\ListBuilder;
use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageUserTechnicianComment;
use plugin\ycMassage\utils\enum\Examine;
use support\Request;

class TechnicianCommentController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->addScreen('name', 'input', '技师', [
            'placeholder' => '技师姓名搜索'
        ]);
        $builder->addScreen('content', 'input', '内容', [
            'placeholder' => '内容搜索'
        ]);
        $builder->addScreen('reply', 'input', '回复', [
            'placeholder' => '回复搜索'
        ]);
        $builder->addScreen('examine', 'select', '审核', [
            'options' => Examine::getOptions()
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
            ->addColumnEle('headimg', '头像', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('nickname', '昵称', [
                'width' => 120,
            ])
            ->addColumn('name', '技师', [
                'width' => 100
            ])
            ->addColumnEle('technician_headimg', '头像', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('technician_nickname', '昵称', [
                'width' => 120,
            ])
            ->addColumn('content', '内容', [])
            ->addColumnEle('images', '图片', [
                'width' => 200,
                'params' => [
                    'type' => 'images',
                ],
            ])
            ->addColumn('reply', '技师回复', [])
            ->addColumn('grade', '评分', [
                'width' => 100
            ])
            ->addColumnEle('anony', '匿名', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => [
                        0 => '否',
                        1 => '是'
                    ],
                    'style' => [
                        0 => [
                            'type' => 'info'
                        ],
                        0 => [
                            'type' => 'danger'
                        ]
                    ],
                ]
            ])
            ->addColumnEle('examine', '审核', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => Examine::dictOptions(),
                    'style' => Examine::dictOptions('style')
                ],
            ])
            ->addColumnEle('show', '显示', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => [
                        0 => '否',
                        1 => '是'
                    ],
                ],
            ]);
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['c.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if ($uid) {
            $where[] = ['c.uid', '=', $uid];
        }
        $name = $request->get('name');
        if ($name) {
            $where[] = ['t.name', 'like', "%{$name}%"];
        }
        $content = $request->get('content');
        if ($content) {
            $where[] = ['c.content', 'like', "%{$content}%"];
        }
        $reply = $request->get('reply');
        if ($reply) {
            $where[] = ['c.reply', 'like', "%{$reply}%"];
        }
        $examine = $request->get('examine');
        if ($examine !== null) {
            $where[] = ['c.examine', '=', $examine];
        }
        if ($request->use_mch_id) {
            $where[] = ['t.work_mch_id', '=', $request->use_mch_id];
        } else {
            $where[] = ['t.work_shop_id', '=', $request->use_shop_id];
        }
        $data = MassageUserTechnicianComment::where($where)
            ->alias('c')
            ->join('MassageUserTechnician t', 't.uid = c.technician_id')
            ->join('MassageUser ut', 'ut.id = c.technician_id')
            ->join('MassageUser u', 'u.id = c.uid')
            ->order('c.create_time desc')
            ->field('c.*,t.name,u.nickname,u.headimg,ut.nickname as technician_nickname,ut.headimg as technician_headimg')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->nickname = base64_decode($item->nickname);
                if (strpos($item->headimg, 'http') !== 0) {
                    $item->headimg = UploadService::url($item->headimg);
                }
                $item->technician_nickname = base64_decode($item->technician_nickname);
                if (strpos($item->technician_headimg, 'http') !== 0) {
                    $item->technician_headimg = UploadService::url($item->technician_headimg);
                }
            });
        return $this->successRes($data);
    }
}
